package com.example.finger.adpter;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.finger.FingerHelper;
import com.example.finger.R;
import com.example.finger.dialog.TaskInfoDialog;
import com.example.finger.bean.TaskSheet;
import com.google.gson.Gson;
import com.lyxmaster.lib.util.DataConfig;

import java.util.Date;
import java.util.List;

/**
 * Created by LYX on 2018/11/23.
 */

public class TaskAdapter extends BaseAdapter {
    private List<TaskSheet> list;
    private Context context;

    public TaskAdapter(List<TaskSheet> list, Context context){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.adpter_task, null);

            viewHolder = new ViewHolder();
            viewHolder.textViewName = convertView.findViewById(R.id.tv_name);
            viewHolder.textViewLock = convertView.findViewById(R.id.tv_lock);
            viewHolder.textViewId = convertView.findViewById(R.id.tv_id);
            viewHolder.textViewLockNum = convertView.findViewById(R.id.tv_lock_num);
            viewHolder.imageView = convertView.findViewById(R.id.imageview);
            viewHolder.imageViewTip = convertView.findViewById(R.id.imageview_tip);

            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final TaskSheet taskSheet = list.get(position);
        viewHolder.textViewName.setText("军官：" + taskSheet.getUsera().size() + " 位 - 士兵 " + taskSheet.getUserb().size() + " 位");
//        String[] nums = taskSheet.getTask().getLocknum().split(" ");
        viewHolder.textViewLock.setText(taskSheet.getTask().getBuweiname());
        viewHolder.textViewId.setText(taskSheet.getTask().getBuweinumber());
        viewHolder.textViewLockNum.setText("挂锁：" + taskSheet.getPadLock().size() + " 把");

        long dateNow = new Date().getTime();
        long dataStart = DataConfig.strToDateLong(taskSheet.getTask().getTimestart()).getTime();
        long dataEnd = DataConfig.strToDateLong(taskSheet.getTask().getTimeend()).getTime();

        if(dateNow >= dataStart && dateNow <= dataEnd && taskSheet.getTask().getTimeStatust() != -100){
            viewHolder.imageViewTip.setVisibility(View.GONE);
        }else {
            viewHolder.imageViewTip.setVisibility(View.VISIBLE);
        }

        viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TaskInfoDialog taskInfoDialog = TaskInfoDialog.getInstance(new Gson().toJson(taskSheet));
                taskInfoDialog.show(((FragmentActivity)(context)).getSupportFragmentManager(),"taskinfo" + position);
            }
        });

        return convertView;
    }

    class ViewHolder {
        public TextView textViewName;
        public TextView textViewLock;
        public TextView textViewId;
        public TextView textViewLockNum;
        public ImageView imageView;
        public ImageView imageViewTip;
    }
}
